package net.hn.hnms.biz.mine.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.api.mine.model.MineVo;
import net.hn.hnms.biz.api.mine.model.SysModelVo;
import net.hn.hnms.biz.api.safety.monitor.model.ComprehensiveScore;
import net.hn.hnms.biz.mine.domain.DimCompanyCollieryCoal;
import net.hn.hnms.biz.mine.domain.DimModel;
import net.hn.hnms.biz.api.mine.model.WeChartBaseMessage;
import net.hn.hnms.biz.mine.domain.WeChartMessage;
import net.hn.hnms.biz.mine.repository.DimCompanyCollieryCoalRepository;
import net.hn.hnms.biz.mine.repository.DimModelRepository;
import net.hn.hnms.biz.mine.service.GasService;
import net.hn.hnms.biz.mine.service.MessageService;
import net.hn.hnms.biz.mine.vo.BroadcastVo;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.common.satoken.utils.LoginHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Author: PanSX
 * Date: 2022/8/5 14:43
 */
@RestController
@RequestMapping("mine/info")
@Api(value = "煤矿信息控制器", tags = {"煤矿信息"})
public class MineInfoController {

    @Autowired
    private DimCompanyCollieryCoalRepository dimCompanyCollieryCoalRepository;
    @Autowired
    private DimModelRepository dimModelRepository;
    @Autowired
    private MessageService messageService;
    @Autowired
    private GasService gasService;

    @GetMapping("detail")
    @ApiOperation("煤矿信息")
    @AutoSetMineCode
    public R<MineVo> getMineInfo(String mineCode) {
        DimCompanyCollieryCoal byCoalMineCode = dimCompanyCollieryCoalRepository.findByCoalMineCode(mineCode);
        if (byCoalMineCode == null) {
            return R.ok(new MineVo());
        } else {
            MineVo mineVo = new MineVo();
            BeanUtils.copyProperties(byCoalMineCode, mineVo);
            return R.ok(mineVo);
        }
    }

    @GetMapping("getSysModel")
    @ApiOperation("煤矿信息")
    public R<SysModelVo> getSysModel(@RequestParam("sysName") String sysName) {
        Optional<DimModel> byId = dimModelRepository.findById(sysName);
        SysModelVo sysModelVo = new SysModelVo();
        if (byId.isPresent()) {
            BeanUtils.copyProperties(byId.get(), sysModelVo);
        }
        return R.ok(sysModelVo);
    }

    @PostMapping("/sendMessage/add")
    @ApiOperation("添加发送消息")
    public R<WeChartMessage> sendMessage(@RequestBody @Validated WeChartMessage msg) {
        if (!"hn@2022".equals(msg.getSendPwd())) {
            return R.fail(400, "发送密码错误");
        }
        if (msg.getStationCodes().isEmpty() && msg.getUserIds().isEmpty()) {
            return R.fail(400, "接收者不可为null");
        }
        msg.setCreatUser(LoginHelper.getUserId());
        msg.setCreatTime(new Date());
        WeChartMessage weChartMessage = null;
        try {
            weChartMessage = messageService.sendMessage(msg);
            return R.ok(weChartMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }

    }

    @PostMapping("/sendBaseMessage")
    @ApiOperation("发送自定义消息")
    public R sendBaseMessage(@RequestBody @Validated WeChartBaseMessage msg) {
        if (!"hn@2022".equals(msg.getPassword())) {
            return R.fail(400, "发送密码错误");
        }
        JSONObject jsonObject = messageService.wechatBaseMessage(msg);
        return R.ok(jsonObject);

    }

    @GetMapping("/broadcast/select")
    @ApiOperation("广播终端下拉框")
    public R<List<BroadcastVo>> BroadcastSelect() {
        List<BroadcastVo> list = new ArrayList<>();
        list.add(new BroadcastVo("10.10.23.116", "7011", "副井井口"));
        list.add(new BroadcastVo("10.10.23.117", "7012", "驱动机房内"));
        list.add(new BroadcastVo("10.10.23.114", "7010", "主井皮带机对接"));
        list.add(new BroadcastVo("10.10.23.115", "7013", "副中底部车场"));
        list.add(new BroadcastVo("10.10.23.122", "7209", "250808皮带机头"));
        list.add(new BroadcastVo("10.10.23.123", "7210", "2502皮带延伸巷机头"));
        list.add(new BroadcastVo("10.10.23.126", "7215", "架空行人十号联络巷"));
        list.add(new BroadcastVo("10.10.23.128", "7214", "250808设备操作室对接"));
        list.add(new BroadcastVo("10.10.23.130", "7208", "250207车场"));
        list.add(new BroadcastVo("10.10.23.131", "7211", "250208车场"));
        list.add(new BroadcastVo("10.10.23.132", "7207", "2502采区变电所"));
        list.add(new BroadcastVo("10.10.23.134", "7221", "250201车场"));
        list.add(new BroadcastVo("10.10.23.135", "7222", "250203救生舱"));
        list.add(new BroadcastVo("10.10.23.137", "7218", "四三联络航口"));
        list.add(new BroadcastVo("10.10.23.138", "7216", "二水平消防材料库"));
        list.add(new BroadcastVo("10.10.23.139", "7212", "五四联络航口"));
        list.add(new BroadcastVo("10.10.23.140", "7217", "五四避难硐室"));
        list.add(new BroadcastVo("10.10.23.143", "7206", "架空行人一号巷"));
        list.add(new BroadcastVo("10.10.23.144", "7205", "辅运巷一号联络巷"));
        list.add(new BroadcastVo("10.10.23.145", "7204", "二水平瓦斯抽放硐室"));
        list.add(new BroadcastVo("10.10.23.146", "7205", "2502辅运巷车场"));
        list.add(new BroadcastVo("10.10.23.147", "7200", "1072下部车场"));
        list.add(new BroadcastVo("10.10.23.149", "7202", "860中变"));
        list.add(new BroadcastVo("10.10.23.150", "7020", "南风井口"));
        list.add(new BroadcastVo("10.10.23.151", "7018", "106人车等候硐室"));
        list.add(new BroadcastVo("10.10.23.152", "7022", "快速通道"));
        list.add(new BroadcastVo("10.10.23.154", "7025", "1050中变"));
        list.add(new BroadcastVo("10.10.23.156", "7017", "1072绞车房"));
        list.add(new BroadcastVo("10.10.23.158", "7019", "1072上部车场"));
        list.add(new BroadcastVo("10.10.23.159", "7015", "副中环形车场口"));
        list.add(new BroadcastVo("10.10.23.160", "7016", "1050平运机头"));
        list.add(new BroadcastVo("10.10.23.162", "7032", "一水平炸药库门口"));
        list.add(new BroadcastVo("10.10.23.164", "7031", "2502皮带机头对接"));
        list.add(new BroadcastVo("10.10.23.182", "7108", "1504下运皮带机头对接"));
        list.add(new BroadcastVo("10.10.23.181", "7107", "150403设备列车对接"));
        list.add(new BroadcastVo("10.10.23.180", "7106", "1504平运皮带机尾"));
        list.add(new BroadcastVo("10.10.23.179", "7112", "150403回风巷口"));
        list.add(new BroadcastVo("10.10.23.178", "7111", "1172车场"));
        list.add(new BroadcastVo("10.10.23.177", "7110", "1504瓦斯抽放硐室"));
        list.add(new BroadcastVo("10.10.23.176", "7109", "1504绞车房"));
        list.add(new BroadcastVo("10.10.23.169", "7105", "1112车场"));
        list.add(new BroadcastVo("10.10.23.167", "7102", "1504下部车场"));
        list.add(new BroadcastVo("10.10.23.165", "7100", "1504采变区变电所"));
        return R.ok(list);
    }

    @GetMapping("getComprehensiveScore")
    @AutoSetMineCode
    @ApiOperation("综合概览评分")
    public R<ComprehensiveScore> getComprehensiveScore(String mineCode) {
        return R.ok(gasService.score(mineCode));
    }
}
